package com.tuanzi.loan.web.bean.family;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.common.CommProductId;
import com.tuanzi.loan.business.service.family.SwitchService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.view.family.SwitchInfoView;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Debug
@Slf4j
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class SwitchInfoBean extends JSFInfoBean<SwitchInfoView> {

    @Autowired
    private SwitchService switchService;

    @PostConstruct
    private void init() {
        String id = getParamFromRequest("id");
        String[] params = StringUtils.split(id, "-");
        String parentProductId = params[0];
        String switchModule = params[1];
        String switchName = params[2];
        $ = new SwitchInfoView(parentProductId, switchModule, switchName);

        List<String> productIds = switchService.findBy($);
        $.setProductIds(productIds);
    }

    public void onSave() {
        switchService.openSwitch($);
        changeStatusToView();
        addSuccessfullyMessage();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.LARGE_COMMON_PRODUCT, DictionaryCode.SWITCH_MODULE, DictionaryCode.SWITCH_NAME);
    }

    @Override
    public List<CommProductId> getCommProductIds() {
//        return super.getCommProductIds().stream().filter(commProductId -> commProductId.getParentId().equals($.getParentProductId()) && commProductId.getRealPrdid() != null).collect(Collectors.toList());
        return getAppCommProductIds().stream().filter(commProductId -> commProductId.getParentId().equals($.getParentProductId()) && commProductId.getRealPrdid() != null).collect(Collectors.toList());
    }

    public List<String> getShowProducts() {
        if (CollectionUtils.isEmpty($.getProductIds())) {
            return Lists.newArrayList();
        }

        return getCommProductIds().stream()
                .filter(commProductId -> $.getProductIds().contains(commProductId.getProductId().toString()))
                .map(commProductId -> String.format("%s(%s)", commProductId.getName(), commProductId.getProductId()))
                .collect(Collectors.toList());
    }

    public void setShowProducts(List<String> products) {
        List<String> productIds = products.stream().map(product -> product.substring(product.lastIndexOf('(')+1, product.lastIndexOf(')'))).collect(Collectors.toList());
        $.setProductIds(productIds);
    }
}
